// mycollection.js
var shihan = require('../../../common/fetch');
var config = require('../../../common/config');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    page_size: 20,
    currentTab: 0,
    favoritelistflag: true,
    showLoading: true,
    favoritelist: [],
    res_navbar: ['商品', '店铺'],
    defaults: 'https://img-oss.shihanbainian.com/shihan2/index/defaults.png'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) { //favorite_video_items
    console.log(config)
    this.publicinfo()
    this.changefavoritelist(this.data.currentTab);
  },
  changefavoritelist: function(tabindex) {
    if (config.video_menu_show == 0) {
      switch (Number(tabindex)) {
        case 0:
          this.refreshfavoritelist(config.favorite_product, 'sh_products')
          break;
        case 1:
          this.refreshfavoritelist(config.favorite_shop, 'sh_shops')
          break;
      }
    } else {
      switch (Number(tabindex)) {
        case 0:
          this.refreshfavoritelist(config.favorite_product, 'sh_products')
          break;
        case 1:
          this.refreshfavoritelist(config.favorite_shop, 'sh_shops')
          break;
        case 2:
          this.refreshfavoritelist(config.favorite_video_items, 'sh_videos')
          break;
      }
    }
  },
  refreshfavoritelist: function(a, b) {
    var that = this
    shihan.sendRequest(a, 'POST', {
        page: that.data.page,
        page_size: that.data.page_size
      }, {})
      .then(function(response) {
        if (response.data.code == 0 && response.data.data[b].length > 0) {
          if (response.data.data.sh_products) {
            for (var i = 0; i < response.data.data.sh_products.length; i++) {
              if (response.data.data.sh_products[i].sh_label.sh_location) {
                var pos = response.data.data.sh_products[i].sh_label
                pos['posa'] = pos.sh_location.split('_')[0];
                pos['posb'] = pos.sh_location.split('_')[1];
              }
            }
          }
          that.setData({
            page: that.data.page + 1,
            favoritelist: that.data.favoritelist.concat(response.data.data[b]),
            favoritelistflag: true,
            showLoading: false,
            prohasMore: true
          })
        } else {
          that.setData({
            favoritelistflag: false,
            showLoading: false,
            prohasMore: false
          })
        }
      }, function(error) {
        console.log(error);
      });
  },
  onResNavbarTap: function(e) {
    // console.log(e)
    this.setData({
      page: 1,
      favoritelist: [],
      showLoading: true,
      currentTab: e.currentTarget.dataset.index
    });
    if (this.data.favoritelist == 0) {
      this.changefavoritelist(this.data.currentTab);
    }
  },
  viewgoodsDetail: function(e) {
    wx.navigateTo({
      url: '/pages/goodsdetail/goodsdetail?goodsid=' + e.currentTarget.dataset.id
    })
  },
  openstoredetail: function(e) { //e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/storedetail/storedetail?id=' + e.currentTarget.dataset.id
    })
  },
  openvideoddetail: function(e) {
    wx.navigateTo({
      url: '/pages/other/videodetail/videodetail?id=' + e.currentTarget.dataset.id
    })
  },
  publicinfo: function() { //configinfo
    if (config.video_menu_show == 0) {
      this.setData({
        res_navbar: ['商品', '店铺']
      })
    } else {
      this.setData({
        // res_navbar: ['商品', '店铺']
        res_navbar: ['商品', '店铺', "视频"]
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    // this.setData({
    //   page: 1,
    //   favoritelist: [],
    //   showLoading: true
    // });
    // this.changefavoritelist(this.data.currentTab);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this
    if (that.data.favoritelistflag) {
      that.setData({
        favoritelistflag: false
      })
      this.changefavoritelist(this.data.currentTab)
    }
  }
})